package com.lanou.musicbox.activity;

import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.RemoteException;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.lanou.musicbox.R;
import com.lanou.musicbox.base.BaseActivity;
import com.lanou.musicbox.fragment.IMusicBackService;
import com.lanou.musicbox.fragment.MainFragment;
import com.lanou.musicbox.service.MusicService;
/**
 * Created by dllo on 15/10/24.
 */
public class MainActivity extends BaseActivity implements View.OnClickListener {
    private FragmentManager manager;
    private FragmentTransaction transaction;

    public static final String UPDATE_UI = "com.aidl.music.updateui";
    public static final int MSG_UPDATE_UI = 0;
    private ImageView bottom_img;
    private TextView bottom_title;
    private TextView bottom_singer;
    private Button bottom_next;//下一曲
    private Button bottom_pause;//开始
    private Button bottom_up;  //上一曲
    private IMusicBackService mService; // 定义音乐进程
    private UpdateUiBroadcastReceiver mUpdateUiBroadcastReceiver; //定义全局监听器
<<<<<<< HEAD:java/com/lanou/musicbox/activity/MainActivity.java

=======
    private MusicListAdapter adapter;



    // 以下是抽屉的定义内容
    private LinearLayout linear_main;
    private LinearLayout changeSkinID;
    private LinearLayout downloadID;
    private LinearLayout setUpID;
    private DrawerLayout drawerLayout;
>>>>>>> c21263b6fc4af118f33e92bfb192e1f79ae04609:java/com/lanou/musicbox/activity/MainFragmentActivity.java


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
<<<<<<< HEAD:java/com/lanou/musicbox/activity/MainActivity.java
        setContentView(R.layout.activity_main);
        MainFragment fragment = new MainFragment(manager);
        manager = getSupportFragmentManager();
        transaction = manager.beginTransaction();
        transaction.replace(R.id.fill_main, fragment);
        transaction.commit();
=======
        setContentView(R.layout.drawer);
        ButterKnife.bind(this);
        mTabHost = (TabHost) findViewById(android.R.id.tabhost);
        linear_main = (LinearLayout) findViewById(R.id.linear_main);

        // 监听抽屉的整体布局防止点击事件冲突
        linear_main = (LinearLayout) findViewById(R.id.linear_main);
        linear_main.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.e("ZBL", "<---防止点击事件冲突--->");
            }
        });

        // 抽屉的监听用来打开或关闭抽屉
        drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
        drawerLayout.setOnClickListener(this);
>>>>>>> c21263b6fc4af118f33e92bfb192e1f79ae04609:java/com/lanou/musicbox/activity/MainFragmentActivity.java

        changeSkinID.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

        scan = (LinearLayout) findViewById(R.id.scan);
        scan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 点击跳转页面之后要关闭抽屉
                // 关闭抽屉
                 drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
                // 打开抽屉
                drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
            }
        });


        /**
         * 构造方法初始化
         * ***/
        initdata();
        initwork();

<<<<<<< HEAD:java/com/lanou/musicbox/activity/MainActivity.java
=======
        mTabHost.setup();
        //本地音乐设置
        TabHost.TabSpec tab0 = mTabHost.newTabSpec("");
        View tab0View = getLayoutInflater().inflate(R.layout.tab_locality_music, null);
        tab0.setIndicator(tab0View);
        tab0.setContent(R.id.locality_music);
        mTabHost.addTab(tab0);
        //网络音乐设置
        TabHost.TabSpec tab1 = mTabHost.newTabSpec("");
        View tab1view = getLayoutInflater().inflate(R.layout.tab_netork_music, null);
        tab1.setIndicator(tab1view);
        tab1.setContent(R.id.network_music);
        mTabHost.addTab(tab1);
        //电台设置
        TabHost.TabSpec tab2 = mTabHost.newTabSpec("");
        View tab2view = getLayoutInflater().inflate(R.layout.tab_network_radio, null);
        tab2.setIndicator(tab2view);
        tab2.setContent(R.id.network_radio);
        mTabHost.addTab(tab2);
        //搜素设置
        TabHost.TabSpec tab3 = mTabHost.newTabSpec("");
        View tab3view = getLayoutInflater().inflate(R.layout.tab_seek_music, null);
        tab3.setIndicator(tab3view);
        tab3.setContent(R.id.seek_music);
        mTabHost.addTab(tab3);
        //设置第一个显示的页面
        mTabHost.setCurrentTab(0);
        //初始化 manager
        manager = getSupportFragmentManager();
        //获得 Fragment 的业务对象
        transaction = manager.beginTransaction();
        //实例化 业务对象
        LocalityFragment localityFragment = new LocalityFragment();
        NetworkMusicFragment networkMusicFragment = new NetworkMusicFragment();
        NetworkRadioFragment networkRadioFragment = new NetworkRadioFragment();
        SeekMusicFragment seekMusicFragment = new SeekMusicFragment();
        // 替换占位符
        transaction.replace(R.id.locality_music, localityFragment);
        transaction.replace(R.id.network_music, networkMusicFragment);
        transaction.replace(R.id.network_radio, networkRadioFragment);
        transaction.replace(R.id.seek_music, seekMusicFragment);
        //添加
        transaction.commit();

    }

    // 这是一个fragment与fragment之间传值的过程
    public void jump(String data) {
        getSupportFragmentManager().beginTransaction().replace(R.id.network_music, new EnterTheSongsFragment(data)).addToBackStack(null).commit();
>>>>>>> c21263b6fc4af118f33e92bfb192e1f79ae04609:java/com/lanou/musicbox/activity/MainFragmentActivity.java
    }
    /**
     * 本地音乐
     */
    private void initwork() {
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(UPDATE_UI);
        mUpdateUiBroadcastReceiver = new UpdateUiBroadcastReceiver();//全局监听
        registerReceiver(mUpdateUiBroadcastReceiver, intentFilter);
        //启动Service
        startService(new Intent(this, MusicService.class));
    }

    /**
     * 获取IMusicBackService对象，当调用bindService会执行onServiceConnected，
     */
    private ServiceConnection mRemoteConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            mService = IMusicBackService.Stub.asInterface(service);
        }
        //服务连接不成功
        @Override
        public void onServiceDisconnected(ComponentName name) {
            mService = null;
        }
    };

    @Override
    public void onStart() {
        bindService(new Intent(this, MusicService.class), mRemoteConnection, Context.BIND_AUTO_CREATE);
        super.onStart();
    }

    //停止
    @Override
    public void onStop() {
        super.onStop();
    }

    //销毁
    @Override
    public void onDestroy() {
        unregisterReceiver(mUpdateUiBroadcastReceiver);
        try {
            if (!mService.getMusicIsPlaying()) {
                stopService(new Intent(this, MusicService.class));
            }
            unbindService(mRemoteConnection);
        } catch (RemoteException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        super.onDestroy();
    }


    /***
     * 构造方法
     * 初始化id
     **/
    private void initdata() {
<<<<<<< HEAD:java/com/lanou/musicbox/activity/MainActivity.java
=======
        //初始化所有数据

        scan = (LinearLayout) findViewById(R.id.scan);
        scan.setOnClickListener(this);
        changeSkinID = (LinearLayout) findViewById(R.id.changeSkinID);
        changeSkinID.setOnClickListener(this);
        downloadID = (LinearLayout) findViewById(R.id.downloadID);
        downloadID.setOnClickListener(this);
        setUpID = (LinearLayout) findViewById(R.id.setUpID);
        setUpID.setOnClickListener(this);
>>>>>>> c21263b6fc4af118f33e92bfb192e1f79ae04609:java/com/lanou/musicbox/activity/MainFragmentActivity.java

        bottom_img = (ImageView) findViewById(R.id.bottom_img);
        bottom_title = (TextView) findViewById(R.id.bottom_title);
        bottom_singer = (TextView) findViewById(R.id.bottom_singer);
        bottom_next = (Button) findViewById(R.id.bottom_next);
        bottom_pause = (Button) findViewById(R.id.bottom_pause);
        bottom_up = (Button) findViewById(R.id.bottom_up);
        bottom_next.setOnClickListener(this);
        bottom_pause.setOnClickListener(this);
        bottom_up.setOnClickListener(this);
    }
<<<<<<< HEAD:java/com/lanou/musicbox/activity/MainActivity.java
=======


>>>>>>> c21263b6fc4af118f33e92bfb192e1f79ae04609:java/com/lanou/musicbox/activity/MainFragmentActivity.java
    @Override
    public void onClick(View view) {
        switch (view.getId()) {

<<<<<<< HEAD:java/com/lanou/musicbox/activity/MainActivity.java
=======
            case R.id.changeSkinID:
                mTabHost.setCurrentTab(1);
                break;
            case R.id.downloadID:
                mTabHost.setCurrentTab(2);
                break;
            case R.id.setUpID:
                mTabHost.setCurrentTab(3);
                break;

>>>>>>> c21263b6fc4af118f33e92bfb192e1f79ae04609:java/com/lanou/musicbox/activity/MainFragmentActivity.java
            case R.id.bottom_pause:
                try {
                    if (mService.getMusicIsPlaying()) {
                        bottom_pause.setBackgroundResource(R.drawable.bt_play);
                        mService.stopMusic();
                    } else {
                        bottom_pause.setBackgroundResource(R.drawable.bt_pause);
                        mService.playMusic();
                    }
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.bottom_next:
                try {
                    mService.PlayNextMusic();
                } catch (RemoteException e) {
                    e.printStackTrace();
                }

                break;
            case R.id.bottom_up:
                try {
                    mService.playUpMusic();
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
                break;
        }

<<<<<<< HEAD:java/com/lanou/musicbox/activity/MainActivity.java
=======

        // 关闭抽屉
        drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
        // 打开抽屉
        drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);

>>>>>>> c21263b6fc4af118f33e92bfb192e1f79ae04609:java/com/lanou/musicbox/activity/MainFragmentActivity.java
    }


    /**
     * 当前播放歌曲改变时。activity界面更新
     *
     * @throws RemoteException
     */
    private void updateUi() throws RemoteException {
//       bottom_title.setText(adapter.getArtist(mService.getNowPlay()));
//       bottom_singer.setText(adapter.getMusicName(mService.getNowPlay()));
        if (mService.getMusicIsPlaying()) {
            bottom_pause.setBackgroundResource(R.drawable.bt_pause);
        } else {
            bottom_pause.setBackgroundResource(R.drawable.bt_play);
        }
    }

    //根据接收的消息,处理UI更新
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MSG_UPDATE_UI:
                    try {
                        updateUi();
                    } catch (RemoteException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    break;
            }
            super.handleMessage(msg);
        }
    };

    //接收到发过来的广播
    public class UpdateUiBroadcastReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub
            handler.sendEmptyMessage(MSG_UPDATE_UI);
        }
    }


}
